package com.util;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @author: cxt
 * @time: 2021/2/20
 */
public class JsonReadFile {
    public static void main(String[] args) throws IOException {
        String s = readJsonFile("E:\\district.json");

        // 将读取到的json数据转成list
//        List<JsonBean> jsonBeans = JSONArray.parseArray(s, JsonBean.class);

//        assert jsonBeans != null;
//        for (JsonBean jsonBean : jsonBeans) {
//            JsonBean[] children = jsonBean.getChildren();
//            if (children != null && children.length != 0) {
//                for (JsonBean child : children) {
//                    child.setPid(jsonBean.getId());
//                    JsonBean[] ch = child.getChildren();
//                    if (ch != null && ch.length != 0) {
//                        for (JsonBean bean : ch) {
//                            bean.setPid(child.getId());
//                        }
//                    }
//                }
//            }
//        }

        // 将json对象转成字符串 // jsonBeans json对象
//        String json = com.paladin.framework.utils.convert.JsonHandle.getJson(jsonBeans);
//        System.out.println(json);
    }

    //读取json文件成字符串
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), StandardCharsets.UTF_8);
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
